﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DAL.EntityDataModel;
using System.Diagnostics;
using System.Data;
using BLL.Theme;


namespace BLL.Formation
{
    public class FormationBLL
    {
        /*****************************
         * Public BLL Method
         *****************************/

        /// <summary>
        /// Return all formations
        /// </summary>
        /// <returns></returns>
        public static List<FormationBO> GetFormations()
        {
            List<FormationBO> formationsBO = new List<FormationBO>();
            List<DAL.EntityDataModel.Formation> formations = new List<DAL.EntityDataModel.Formation>();
            
            using (DBEntities db = new DBEntities())
            {

                try
                {   
                    formations = db.Formations.ToList();

                    formationsBO = FormationBLL.CopyeEntitiesToBOlist(formations);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }

            }

            return formationsBO;
        }

        /// <summary>
        /// Add a new formation 
        /// </summary>
        /// <param name="bo"></param>
        public static void AddFormation(FormationBO bo)
        {
            DAL.EntityDataModel.Formation entity = new DAL.EntityDataModel.Formation();
            FormationBLL.CopyBoToEntity(bo, entity);

            using (DBEntities db = new DBEntities())
            {
                try
                {
                    db.Formations.AddObject(entity);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }    
            }
        }

        /// <summary>
        /// Delete a formation
        /// </summary>
        /// <param name="bo"></param>
        public static void DeleteFormation(FormationBO bo)
        {
            DAL.EntityDataModel.Formation entity = new DAL.EntityDataModel.Formation();

            using (DBEntities db = new DBEntities())
            {
                try
                {
                    entity = db.Formations.FirstOrDefault(f => f.Identifiant == bo.Identifiant);
                    FormationBLL.CopyBoToEntity(bo, entity);
                    db.Formations.DeleteObject(entity);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

            
        }


        public static void UpdateFormation(FormationBO bo)
        {
            DAL.EntityDataModel.Formation entity = new DAL.EntityDataModel.Formation();
            
            using (DBEntities db = new DBEntities())
            {
                try
                {
                    entity =  db.Formations.FirstOrDefault(f => f.Identifiant == bo.Identifiant) ;
                    FormationBLL.CopyBoToEntity(bo, entity);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
        }

        /*****************************
         * Public traitement Method
         *****************************/

        /// <summary>
        /// Copy a entity to a business object
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static FormationBO CopyEntityToBO(DAL.EntityDataModel.Formation entity)
        {
            FormationBO bo = new FormationBO();

            try
            {
                bo.Description = entity.Description;
                bo.Identifiant = entity.Identifiant;
                bo.Libelle = entity.Libelle;
                bo.NombreJours = entity.NombreJours;
                bo.Prix = (double)entity.Prix;
                bo.Reference = entity.Reference;
                //bo.Theme = new ThemeBO() { Identifiant = entity.Theme.Identifiant, Libelle = entity.Theme.Libelle };
                bo.Theme = ThemeBLL.CopyEntityToBO(entity.Theme);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return bo;
        }

        /// <summary>
        /// Copy Business to entity
        /// </summary>
        /// <param name="bo"></param>
        /// <returns></returns>
        public static void CopyBoToEntity(FormationBO bo, DAL.EntityDataModel.Formation entity )
        {
            try
            {
                if (bo.Identifiant != 0)
                    entity.Identifiant = bo.Identifiant;

                entity.Description = bo.Description;
                entity.IdTheme = bo.Theme.Identifiant;
                entity.Libelle = bo.Libelle;
                entity.NombreJours = bo.NombreJours;
                entity.Prix = (decimal)bo.Prix;
                entity.Reference = bo.Reference;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        /// <summary>
        /// Copy entities to a list of business object
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public static List<FormationBO> CopyeEntitiesToBOlist(List<DAL.EntityDataModel.Formation> entities)
        {
            List<FormationBO> boList = new List<FormationBO>();

            try
            {
                foreach (DAL.EntityDataModel.Formation entity in entities)
                {
                    FormationBO bo = FormationBLL.CopyEntityToBO(entity);
                    boList.Add(bo);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return boList;
        }
    }
}
